/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wider.foundation.channel;

import com.wider.foundation.DLogger;
import com.wider.foundation.DLoggerRoot;
import com.wider.foundation.concurrency.Cancellable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author William Choi
 */
public class ChannelTester{


    public static void main(String args[]) throws InterruptedException{

        //Inner-class
        class ChannelHandler implements NetChannelResponseHandler{
            private DLoggerRoot rootlog;

            public ChannelHandler(DLoggerRoot logger){
                rootlog = logger;
            }

            @Override
            public void handle(String given, int resp_code, String resp_mesg, InputStream stream_in) throws IOException {
                rootlog.debug(given + ": ret code " + resp_code + " " + resp_mesg);
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream_in));
                String line;
                while (null != (line = reader.readLine())){
                    rootlog.debug(given + ": " + line);
                }
            }

            @Override
            public void handle(String given, Throwable ex) {
                rootlog.warn(ex);
            }
        }
        HttpChannels channels = null;

        DLoggerRoot logger = DLogger.getRoot();
        logger.setStream(System.out);
        logger.setLevel(DLogger.DLevel.INFOR);
        logger.enable();

        logger.warn("Hello Channel Tester");
        
        ChannelHandler handler = new ChannelHandler(logger);
       
        try {
            channels = new HttpChannels("ChannelGroup-0");
            channels.init(2, 2);

            //Test-1: Possitive Test. Test if echo is successful or not
            HttpChannel channel1 = channels.openChannel("http://localhost:8080/wider_foundation/EchoServlet");

            HashMap<Object, Object> reqst1 = new HashMap<Object, Object>();

            reqst1.put("command", "sleep");
            reqst1.put("timeout", "3000");
            channel1.get(reqst1, handler, 4000);

            logger.debug("1 Channel1's Active requests: " + channel1.getRequestCount());
            logger.debug("Total active requests: " + channels.getActiveRequestCount());
            logger.debug("Total pending requests: " + channels.getPendingRequestCount());

            //Test-2:: Negative Test. directly cancel the returned ticket during waiting for response (active state)
            Cancellable ticket = channel1.get(reqst1, handler, 4000);
            logger.debug("2 Channels1's Active requests: " + channel1.getRequestCount());
            logger.debug("Total active requests: " + channels.getActiveRequestCount());
            logger.debug("Total pending requests: " + channels.getPendingRequestCount());

            Thread.sleep(1000);
            ticket.cancel();
            Thread.sleep(3000);
            logger.debug("3 Channels1's Active requests: " + channel1.getRequestCount());
            logger.debug("Total active requests: " + channels.getActiveRequestCount());
            logger.debug("Total pending requests: " + channels.getPendingRequestCount());

            //Test-3:: Negative Test. Try to cancel the job which is pending in pool's queue.
            channel1.get(reqst1, handler, 4000);
            channel1.get(reqst1, handler, 4000);
            ticket = channel1.get(reqst1, handler, 4000);
            channel1.get(reqst1, handler, 4000);
            logger.debug("4 Channels1's Active requests: " + channel1.getRequestCount());
            logger.debug("Total active requests: " + channels.getActiveRequestCount());
            logger.debug("Total pending requests: " + channels.getPendingRequestCount());
            Thread.sleep(500);
            //ticket.cancel();
            channel1.disconnect(0, TimeUnit.DAYS);
            Thread.sleep(1000);
            logger.debug("5 Channels1's Active requests: " + channel1.getRequestCount());
            logger.debug("Total active requests: " + channels.getActiveRequestCount());
            logger.debug("Total pending requests: " + channels.getPendingRequestCount());
        } catch (MalformedURLException ex) {
            logger.warn(ex);
        } catch (IOException ex) {
            logger.warn(ex);
        } finally {
            //Final termination //normal
            if (null != channels){
                channels.close(0, TimeUnit.DAYS);
            }
        }
    }

}
